﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public class ProfessorObject:ViewableObject
{
    protected new ProfessorObject.Type thisType;
    protected Animation pushButton;
    protected Animation pushButton2;
    protected Animation facePalm;
    protected Animation point;
    protected QuinHero toCreate;
    protected IncubatorObject createWith;
    protected bool resurrecting = false;
    protected bool creatingDone = false;
    protected bool startedCreating = false;
    protected enum resStage
    {
        None, FacePalm, ButtonPush, ButtonRelease
    }
    protected resStage currStage = resStage.None;
    protected double delay = 0;

    public ProfessorObject(ProfessorObject.Type type, Vector3 placement)
        : base(type, placement)
    {
        thisType = type;
        initAnimations();
    }

    private void initAnimations()
    {
        pushButton = new Animation("button", 6, false);//356x987
        for (int i = 0; i < 564; i += 141)
        {
            for (int j = 0; j < 356; j += 89)
            {
                pushButton.addFrame(
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PROFESSOR).get("profButton"),
                        new Rectangle(j, i, 89, 141),
                        0,
                        new Vector2(55, 140)));
            }
        }

        pushButton2 = new Animation("button2", 6, false);//356x987
        for (int i = 564; i < 987; i += 141)
        {
            for (int j = 0; j < 356; j += 89)
            {
                pushButton2.addFrame(
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PROFESSOR).get("profButton"),
                        new Rectangle(j, i, 89, 141),
                        0,
                        new Vector2(55, 140)));
            }
        }

        facePalm = new Animation("palm", 6, false);//320x282
        for (int i = 0; i < 282; i += 141)
        {
            for (int j = 0; j < 320; j += 80)
            {
                facePalm.addFrame(
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PROFESSOR).get("profFacePalm"),
                        new Rectangle(j, i, 80, 141),
                        0,
                        new Vector2(40, 141)));
            }
        }

        point = new Animation("point", 6, false);//376x282
        for (int i = 0; i < 282; i += 141)
        {
            for (int j = 0; (i < 141 && j < 376) || j < 188; j += 94)
            {
                point.addFrame(
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PROFESSOR).get("profPoint"),
                        new Rectangle(j, i, 94, 141),
                        0,
                        new Vector2(32, 141)));
            }
        }
    }

    public void startResurrection()
    {
        resurrecting = true;
    }

    public Vector3 resurrect(QuinHero hero, IncubatorObject incubator)
    {
        toCreate = hero;
        createWith = incubator;
        return createWith.createPlacement(hero);
    }

    public bool isResurrected()
    {
        if (creatingDone)
        {
            creatingDone = false;
            startedCreating = false;
            return true;
        }
        else
        {
            return false;
        }
    }

    public override void update(double elapsed)
    {
        if (resurrecting)
        {
            switch (currStage)
            {
                case resStage.None:
                    currentAnimation = facePalm;
                    currStage = resStage.FacePalm;
                    break;
                case resStage.FacePalm:
                    if (currentAnimation.isFinished)
                    {
                        currentAnimation.setCurrentFrame(0);
                        currentAnimation.isFinished = false;
                        currentAnimation = pushButton;
                        currStage = resStage.ButtonPush;
                    }
                    break;
                case resStage.ButtonPush:
                    if (currentAnimation.isFinished)
                    {
                        currentAnimation.setCurrentFrame(0);
                        currentAnimation.isFinished = false;
                        currentAnimation = pushButton2;
                        currStage = resStage.ButtonRelease;
                        createWith.startCreating();
                        startedCreating = true;
                    }
                    break;
                case resStage.ButtonRelease:
                    if (currentAnimation.isFinished)
                    {
                        currentAnimation.setCurrentFrame(0);
                        currentAnimation.isFinished = false;
                        currentAnimation = stasis;
                        currStage = resStage.None;
                        resurrecting = false;
                    }
                    break;
            }
        }
        if (startedCreating && !creatingDone)
        {
            creatingDone = createWith.isCreatingDone();
        }
        //dont call base.update b/c i dont want to be culled
        currentAnimation.update(elapsed);
        screenCoord = Camera.worldToScreen(origin);
    }

    public new class Type : ViewableObject.Type
    {
        public static readonly Type Professor = new Type(
            ObjectFactory.TypeEnum.Professor,//type
            new Vector3(32, 139, 0),//origin
            new Vector3(65, 139, 60),//collision
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PROFESSOR).get("profStasis"), 
                    new Rectangle(0, 0, 65, 139), 
                    0, 
                    new Vector2(32, 139))});//frameList

        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision,
            ArrayList stasisFrames)
            : base(type, origin, collision, false, stasisFrames, "stasisName", 6, true)
        {
        }

        public static new ProfessorObject.Type getType(ObjectFactory.TypeEnum te)
        {
            switch (te)
            {
                case ObjectFactory.TypeEnum.Professor: return Professor;
            }
            return null;
        }
    }
}
